package com.xbongbong.parent.condition.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.parent.abstracts.AbstractConditionFieldHandler;
import com.xbongbong.parent.field.FieldAnalyseService;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FilterConditionsPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkConditionPoJo;
import com.xbongbong.paas.field.LinkInfoPojo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.pro.businessrule.pojo.BusinessRuleInfoPojo;
import com.xbongbong.pro.condition.pojo.ConditionDTO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/3 19:41
 * @since v1.0
 */
@Component
public class ConditionFormComboCheckHandlerImpl extends AbstractConditionFieldHandler {

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.COMBO_CHECK.getType(), FieldTypeEnum.CHECKBOX_GROUP.getType());
    }


    @Resource
    private Map<Integer, FieldAnalyseService> fieldAnalyseMap;


    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,  FieldAttrEntity targetField,
                                       BusinessRuleInfoPojo businessRuleInfoPojo ) throws XbbException {
        // 根据value获取text
        // 1、如果本表单字段是下拉框、下拉复选、单选按钮、复选按钮，需要先根据value取出text，放入data中；
        // 2、在下方取出data的text，然后拿到对应的value，之后在根据value去查询对应的数据；
        return ItemUtil.getCustomItemList(fieldAttrEntity, data);
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterConditionsPoJo) throws XbbException {
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
            List<String> list = JSON.parseArray(JSON.toJSONString(businessRuleInfoPojo.getObject()), String.class);
            return ItemUtil.getCustomItemList(targetField, list);
        } else {
            return businessRuleInfoPojo.getObject();
        }
    }


    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException {
        Object object = filterCondition.getValue();
        if (object instanceof String) {
            String value = String.valueOf(object);
            List<String> valueList;
            if (value.contains(PaasConstant.CHINESE_SEMICOLON)) {
                String[] valueArray = value.split(PaasConstant.CHINESE_SEMICOLON);
                valueList = Arrays.asList(valueArray);
            } else {
                valueList = Arrays.asList(value);
            }
            // 复选框/下拉复选
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                object = ItemUtil.getCustomItemList(targetField, valueList);
            } else {
                object = valueList;
            }
        } else if (object instanceof List) {
            List<ItemPoJo> itemList = JSON.parseArray(JSON.toJSONString(object), ItemPoJo.class);
            List<Object> valueList = new ArrayList<>();
            itemList.forEach(item->{
                valueList.add(item.getValue());
            });
            object = valueList;
        }
        return object;
    }

    @Override
    public Object handlerRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity relyFieldEntity, LinkConditionPoJo linkConditionPoJo, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), relyFieldEntity.getComboType())) {
            List<ItemPoJo> itemList = relyFieldEntity.getItems();
            Map<Object, Object> itemMap = new HashMap<>(itemList.size());
            itemList.forEach(item -> itemMap.put(item.getText(), item.getValue()));
            List<Map<String, Object>> jsonArray = (ArrayList) value;
            List<Object> valueList = new ArrayList<>();
            if (Objects.nonNull(jsonArray)) {
                for (Object object : jsonArray) {
                    if (object instanceof Map) {
                        Map<String, Object> jsonObject = (Map) object;
                        object = jsonObject.get("text");
                    }
                    if (Objects.nonNull(itemMap.get(object))) {
                        valueList.add(itemMap.get(object));
                    }
                }
            }
            return valueList;
        } else {
            List<Object> valueList = new ArrayList<>();
            List<Map<String, Object>> jsonArray = (ArrayList) value;
            if (Objects.nonNull(jsonArray)) {
                for (Object object : jsonArray) {
                    if (object instanceof Map) {
                        Map<String, Object> jsonObject = (Map) object;
                        object = jsonObject.get("value");
                    }
                    valueList.add(object);
                }
            }
            return valueList;
        }
    }

    @Override
    public Object handlerLinkDataFixedValue(String corpid, FieldAttrEntity relyFieldEntity, LinkInfoPojo linkInfoPojo, Object value) throws XbbException {
        if (value instanceof String) {
            String obj = String.valueOf(value);
            List<String> valueList;
            if (obj.contains(PaasConstant.CHINESE_SEMICOLON)) {
                String[] valueArray = obj.split(PaasConstant.CHINESE_SEMICOLON);
                valueList = Arrays.asList(valueArray);
            } else {
                valueList = Arrays.asList(obj);
            }
            // 复选框/下拉复选
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), relyFieldEntity.getComboType())) {
                value = ItemUtil.getCustomItemList(relyFieldEntity, valueList);
            } else {
                value = valueList;
            }
        } else if (value instanceof List) {
            List<ItemPoJo> itemList = JSON.parseArray(JSON.toJSONString(value), ItemPoJo.class);
            List<Object> valueList = new ArrayList<>();
            itemList.forEach(item -> valueList.add(item.getValue()));
            value = valueList;
        }
        return value;
    }

    @Override
    public Object handlerBusinessRelyFormField(String corpid, FieldAttrEntity fieldAttrEntity, FieldAttrEntity relyFieldEntity, LinkConditionPoJo linkConditionPoJo, Object value, Long linkFormId, Integer linkBusinessType, Integer linkSaasMark) throws XbbException {
        List<Object> textList = JSON.parseArray(JSON.toJSONString(value), Object.class);
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), relyFieldEntity.getComboType())) {
            List<ItemPoJo> itemPoJoList = relyFieldEntity.getItems();
            List<Object> valueList = new ArrayList<>();
            for (ItemPoJo itemPoJo : itemPoJoList) {
                if (textList.contains(itemPoJo.getText())) {
                    valueList.add(itemPoJo.getValue());
                }
            }
            return valueList;
        } else {
            return textList;
        }
    }

    @Override
    public Object handlerBusinessDataFixedValue(String corpid, FieldAttrEntity relyFieldEntity, ConditionDTO conditionDTO, Object value) throws XbbException {
        if (value instanceof String) {
            String obj = String.valueOf(value);
            List<String> valueList;
            if (obj.contains(PaasConstant.CHINESE_SEMICOLON)) {
                String[] valueArray = obj.split(PaasConstant.CHINESE_SEMICOLON);
                valueList = Arrays.asList(valueArray);
            } else {
                valueList = Arrays.asList(obj);
            }
            // 复选框/下拉复选
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), relyFieldEntity.getComboType())) {
                value = ItemUtil.getCustomItemList(relyFieldEntity, valueList);
            } else {
                value = valueList;
            }
        } else if (value instanceof List) {
            List<ItemPoJo> itemList = JSON.parseArray(JSON.toJSONString(value), ItemPoJo.class);
            List<Object> valueList = new ArrayList<>();
            itemList.forEach(item -> valueList.add(item.getValue()));
            value = valueList;
        }
        return value;
    }

    @Override
    public Object handlerPreConditionValue(FieldAttrEntity thisField, BusinessRuleInfoPojo businessRuleInfoPojo, FilterConditionsPoJo filterCondition) throws XbbException{
        FieldAnalyseService fieldAnalyseService = fieldAnalyseMap.get(thisField.getFieldType());
        return fieldAnalyseService.handlerPreConditionValue(thisField, businessRuleInfoPojo, filterCondition);
    }
}
